@page "/k8s/cluster/update/{id:int}"
@using Microsoft.AspNetCore.Components
@using FOPS.Abstract.K8S.Entity
@using FOPS.Abstract.K8S.Server
@using FOPS.Abstract.MetaInfo.Enum
@using FOPS.Abstract.MetaInfo.Server
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;

<div class="layui-card">
    <div class="layui-card-header">
        编辑集群
        <div class="layui-right">
            <button class="layui-btn" @onclick="GotoList"><i class="am-icon-plus"></i> 列表</button>
        </div>
    </div>
    <div class="layui-card-body" style="padding: 15px;">
        <div class="layui-form-item">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-inline">
                <label class="layui-form-label" style="text-align: left">@_info.Id</label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">集群名称</label>
            <div class="layui-input-inline">
                <input type="text" @bind="_info.Name" autocomplete="off" placeholder="请输入集群名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">集群类型</label>
                <div class="layui-input-inline">
                    <select @bind="_info.RuntimeEnvType">
                        @foreach (var eumRuntimeEnv in Enum.GetValues<EumRuntimeEnv>())
                        {
                            <option value="@eumRuntimeEnv.ToString()">@eumRuntimeEnv.ToString()</option>
                        }
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-inline">
                <input type="text" @bind="_info.Sort" autocomplete="off" placeholder="越小越前" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">kubectl配置</label>
            <div class="layui-input-block" style="margin-left: 150px;">
                <textarea @bind="_info.Config" placeholder="请输入kubectl配置" class="layui-textarea" style="margin-top: 0; margin-bottom: 0; height: 400px;"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <button class="layui-btn" @onclick="UpdateInfo">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<style>
.layui-form-label {
    width: 120px;
}
</style>
@code{

    [Parameter] public int Id { get; set; }

    private ClusterVO _info = new() {};

    private async Task UpdateInfo()
    {
        if (Id < 1 || _info == null)
        {
            await js.InvokeVoidAsync("layer.alert", "Yaml模板不存在", new {icon = 2, title = "出错了"});
            GotoList();
        }
        try
        {
            await _iocManager.Resolve<IClusterService>().UpdateAsync(Id, _info);
            await js.InvokeVoidAsync("layer.msg", "修改成功", new {icon = 1});
            GotoList();
        }
        catch (Exception e)
        {
            await js.InvokeVoidAsync("layer.alert", e.Message, new {icon = 2, title = "出错了"});
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (Id < 1 || _info == null)
        {
            await js.InvokeVoidAsync("layer.alert", "Yaml模板不存在", new {icon = 2, title = "出错了"});
            GotoList();
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        if (Id > 0 && _info.Id == 0)
        {
            _info = await _iocManager.Resolve<IClusterService>().ToInfoAsync(Id);
            if (_info == null)
            {
                await js.InvokeVoidAsync("layer.alert", "Yaml模板不存在", new {icon = 2, title = "出错了"});
                GotoList();
            }
        }
    }

    private void GotoList()
    {
        nav.NavigateTo("/k8s/cluster/list");
    }

}